﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ListViewTest
{
    public class MasterImportComparer : IComparer<MasterImport>
    {
        string memberName = string.Empty; // specifies the member name to be sorted
        SortOrder sortOrder = SortOrder.None; // Specifies the SortOrder.

            /// <summary>
        /// constructor to set the sort column and sort order.
        /// </summary>
        /// <param name="strMemberName"></param>
        /// <param name="sortingOrder"></param>
        public MasterImportComparer(string strMemberName, SortOrder sortingOrder)
        {
            memberName = strMemberName;
            sortOrder = sortingOrder;
         }

        public int Compare(MasterImport x, MasterImport y)
        {
            int returnValue = 1;

            foreach (var item in x.GetType().GetProperties())
	        {
                if (item.Name == memberName)
                {
                    foreach (var item2 in y.GetType().GetProperties())
                    {
                        if (item2.Name == memberName)
                            if (sortOrder == SortOrder.Ascending)
                            {
                                if(item.GetValue(x, null).GetType() == typeof(string))
                                    return (item.GetValue(x, null).ToString()).CompareTo((string)item2.GetValue(y, null));

                                if (item.GetValue(x, null).GetType() == typeof(int))
                                {
                                    if ((int)item.GetValue(x, null) > (int)item2.GetValue(y, null))
                                    {
                                      //  ret
                                    }
                                }
                            }
                            else
                            {
                                if (item.GetValue(x, null).GetType() == typeof(string))
                                    return (item2.GetValue(y, null).ToString()).CompareTo((string)item.GetValue(x, null));
                               
                            }

                    }
                }
            }
            
            return returnValue;
        }
    }
}
